package com.lagou.edu.controller;

import com.lagou.edu.dao.ResumeDao;
import com.lagou.edu.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
public class ResumeController {

    /**
     * Spring容器和SpringMVC容器是有层次的（父子容器）
     * Spring容器：service对象+dao对象
     * SpringMVC容器：controller对象，，，，可以引用到Spring容器中的对象
     */
    @Autowired
    private ResumeDao resumeDao;

    @RequestMapping("/resumes")
    public String queryAll(Map<String, Object> map) throws Exception {
        map.put("resumes",resumeDao.findAll());
        return "resume";
    }




    /**
     * 跳转到添加页面input.jsp
     * get 请求
     * @return
     */
    @RequestMapping(value = "/resume", method = RequestMethod.GET)
    public String input(Map<String, Object> map) {
        map.put("resume", new Resume());
        System.out.println("执行input方法，跳转到添加页面input.jsp.....");
        return "input";
    }

    /**
     * 保存Employee操作
     * post 请求
     * @return
     */
    @RequestMapping(value = "/resume", method = RequestMethod.POST)
    public String save(Resume resume) {
        System.out.println("执行save方法，保存添加的数据.....");
        resumeDao.saveAndFlush(resume);
        return "redirect:/resumes";
    }

    /**
     * 删除操作
     * delete 请求
     * @param id
     * @return
     */
    @RequestMapping(value = "/resume/delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable("id") Long id) {
        System.out.println("执行delete方法，删除一条数据.....");
        resumeDao.deleteById(id);
        return "redirect:/resumes";
    }

    /**
     * 转到修改页面input.jsp,并回显数据
     * get 请求
     * @return
     */
    @RequestMapping(value = "/resume/{id}", method = RequestMethod.GET)
    public String input(@PathVariable("id") Long id, Map<String, Object> map) {
        map.put("resume", resumeDao.findById(id).get());
        return "input";
    }

    /**
     * 修改操作
     * put 请求
     * @return
     */
    @RequestMapping(value = "/resume", method = RequestMethod.PUT)
    public String update(Resume resume) {
        System.out.println("执行update方法...");
        resumeDao.save(resume);
        return "redirect:/resumes";
    }

}
